package com.gaoyang.service;

import com.gaoyang.domain.PaiPaiGoods;
import com.gaoyang.domain.PaiPaiIndex;
import com.gaoyang.domain.ZhaoshangUser;
import com.gaoyang.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PaiPaiService {

	private static String PRODUCT_PAIPAI_INDEX_COLLECTION = "paipai_index";
	private static String PRODUCT_PAIPAI_GOODS_COLLECTION = "paipai_goods";

	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
    MongoTemplate mongoTemplate;

	//获取拍拍目录索引表
	public List<PaiPaiIndex> getPaiPaiIndexList() {
		return mongoTemplate.findAll(PaiPaiIndex.class, PRODUCT_PAIPAI_INDEX_COLLECTION);
	}

	public PaiPaiGoods getPaiPaiGoods(String id) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), PaiPaiGoods.class, PRODUCT_PAIPAI_GOODS_COLLECTION);
	}


	//获取拍拍产品表(结束前10分钟的)
	public List<PaiPaiGoods> getPaiPaiGoodsList() {
		Query query = new Query();
		Date date = new Date();
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date);
		calendar1.add(Calendar.MINUTE, -1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date);
		calendar2.add(Calendar.MINUTE, 30);
		query.addCriteria(Criteria.where("id").exists(true)
				.andOperator(
						Criteria.where("endTimeDate").lt(DateUtils.getTimeStr(calendar2)),
						Criteria.where("endTimeDate").gte(DateUtils.getTimeStr(calendar1))));
		return mongoTemplate.find(query, PaiPaiGoods.class, PRODUCT_PAIPAI_GOODS_COLLECTION);
	}

	//保存拍拍产品
	public void savePaiPaiGoods(PaiPaiGoods paiPaiGoods) {
		mongoTemplate.save(paiPaiGoods, PRODUCT_PAIPAI_GOODS_COLLECTION);
	}

	//更新拍拍产品当前价格
	public void updatePaiPaiCurrentPrice(String id, String currentPrice) {
		Query query = new Query(Criteria.where("_id").is(id));
		Update update = Update.update("currentPrice", currentPrice);
		mongoTemplate.updateFirst(query, update, PRODUCT_PAIPAI_GOODS_COLLECTION);
	}

	//更新拍拍产品当前价格
	public void updatePaiPaiIsRemind(String id) {
		Query query = new Query(Criteria.where("_id").is(id));
		Update update = Update.update("isRemind", "1");
		mongoTemplate.updateFirst(query, update, PRODUCT_PAIPAI_GOODS_COLLECTION);
	}



//	public DianPingUser findDianpingUserByTel(String tel) {
//		return mongoTemplate.findOne(new Query(Criteria.where("tel").is(tel)), DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
//	}
//
//	public List<DianPingUser> getDianPingList() {
//		return mongoTemplate.findAll(DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
//	}
//
//	public List<DianPingUser> getDianPingListExecute() {
//		return mongoTemplate.find(new Query(Criteria.where("isExecute").is("1")), DianPingUser.class, PRODUCT_DIANPING_USER_COLLECTION);
//	}
//
//	public List<DianPingUser> getMeiTuanListExecute() {
//		return mongoTemplate.find(new Query(Criteria.where("isExecute").is("1")), DianPingUser.class, PRODUCT_MEITUAN_USER_COLLECTION);
//	}
//
//	public void update(String tel, String column, String value) {
//		Query query = new Query(Criteria.where("tel").is(tel));
//		Update update = Update.update(column, value);
//		this.mongoTemplate.updateFirst(query, update, PRODUCT_DIANPING_USER_COLLECTION);
//	}


}